<?php class Element extends Underscored {
	protected $_Subj = '';
	protected $_Id = '';
	protected $_Timestamp = '';
	protected $_Filename = '';
	protected $_Text = '';
	protected $Loaded = FALSE;

	public function __construct ($Filename) {
		$this->_Filename = $Filename;
		$this->_Id = basename ($this->_Filename);
		$this->_Subj = urldecode ($this->_Id);
		$this->Exists and $this->_Timestamp = filemtime ($this->_Filename);
	}
	protected function __get ($Name) {
		switch ($Name) {
			case 'Exists': return (file_exists ($this->_Filename));
			case 'TextUncached':
				$this->Load ();
			case 'Text':
				$this->Loaded or $this->Load ();
				return ($this->_Text);
			default: return (parent::__get ($Name));
		}
	}

	protected function __set ($Name, $Value) {
		$Me = get_class ($this);
		switch ($Name) {
			case 'Text':
				$this->_Text = $Value;
				if (strlen ($this->_Text)) {
					$this->Save ();
				} else {
					$this->Erase ();
				}
			break;
			default: throw new ErrorException ("Undefined property: $Me::\$$Name");
		}
		return (TRUE);
	}

	protected function Load () {
		if ($this->Exists) {
			$this->_Text = file_get_contents ($this->_Filename);
			$this->Loaded = TRUE;
			return (TRUE);
		} else return (FALSE);
	}
	protected function Save () {
		return (file_put_contents ($this->_Filename, $this->_Text));
	}
	protected function Erase () {
		if ($this->Exists) {
			return (unlink ($this->_Filename));
		} else return (FALSE);
	}
} ?>